package com.woniu.controller;

import com.alibaba.excel.EasyExcel;
import com.woniu.entity.HttpStatus;
import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.QueryUserTrainingDTO;
import com.woniu.pojo.UserTraining;
import com.woniu.service.IUserTrainingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaosong
 * @since 2023-04-01
 */
@RestController
@RequestMapping("/userTraining")
@Slf4j
@Api(tags = "用户靶场")
public class UserTrainingController extends BaseController{
    @Autowired
    private IUserTrainingService userTrainingService;

    @PostMapping("/save")
    @ApiOperation("新增")
    public Result save(@RequestBody UserTraining userTraining){
        boolean u = userTrainingService.save(userTraining);
        return new Result(HttpStatus.SUCCESS,"添加成功",u);
    }
    @PostMapping("/update")
    @ApiOperation("更改")
    public Result update(@RequestBody UserTraining userTraining){
        boolean update = userTrainingService.updateById(userTraining);
        return new Result(HttpStatus.SUCCESS,"修改成功",update);
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Result selectUserTrainingById(@PathVariable String id){
        UserTraining userTraining = userTrainingService.getById(id);
        return toDataResult(userTraining);
    }
    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public Result deleteTrainer(@RequestBody List<String> ids) {
        boolean delete = userTrainingService.removeBatchByIds(ids);
        return toResult(delete);
    }
    @GetMapping("/all")
    @ApiOperation("查询所有")
    public Result selectAllUserTraining() {
        List<UserTraining> userTrainingList = userTrainingService.list();
        return toDataResult(userTrainingList);
    }
    @PostMapping("/search/{currentPage}/{pageSize}")
    @ApiOperation("搜索+分页")
    public Result selectUserTraining(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody QueryUserTrainingDTO userTrainingDTO) {
        PageData pageData = userTrainingService.select(currentPage, pageSize, userTrainingDTO);
        System.out.println(pageData);
        return toDataResult(pageData);
    }
    @ApiOperation("导出数据")
    @PostMapping("/export")
    public void listSearch(@RequestBody(required = false) QueryUserTrainingDTO userTrainingDTO,
                           HttpServletResponse response) throws IOException {
        PageData pageData =  userTrainingService.select(1, 1000, userTrainingDTO);
        List<UserTraining> userTrainingList = (List<UserTraining>) pageData.getData();
        EasyExcel.write(response.getOutputStream(), UserTraining.class).sheet().doWrite(userTrainingList);
    }
}
